<template>
  <el-select v-model="value" class="m-2" placeholder="Select" size="large">
    <el-option
      v-for="item in options"
      :key="item.value"
      :label="item.label"
      :value="item.value"
    />
  </el-select>
  <el-dialog
    class="dialog-body-padding-none dialog-header-display-none"
    v-model="dialogVisible"
    width="1000px"
    :show-close="false"
    :before-close="handleClose"
  >
    <div class="px-4 pt-4 border-b">
      <el-row>
        <el-col :span="24">
          <div class="pb-4">
            <el-input
              v-model="searchValue"
              placeholder="请搜索"
              size="large"
              prefix-icon="Search"
            />
          </div>
        </el-col>
        <el-col :span="12" class="border-r">
          <div class="pr-4">
            <el-tabs
              v-model="activeName"
              type="border-card"
              class="el-tabs--border-line"
              @tab-click="handleClick"
            >
              <el-tab-pane label="常用" name="first">
                <div>
                  <el-tree
                    :data="data"
                    :props="defaultProps"
                    @node-click="handleNodeClick"
                  >
                    <template #default="{ node, data }">
                      <span
                        v-if="data.type === 'dept'"
                        class="custom-tree-node"
                      >
                        <span>{{ node.label }}</span>
                      </span>

                      <div v-else class="user-item small">
                        <div class="user-item-avatar">
                          <el-avatar
                            :size="24"
                            :src="'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'"
                          />
                        </div>
                        <div class="user-item-inf">
                          <div class="inf-name">张三</div>
                          <div class="inf-email">94648929@qq.com</div>
                        </div>
                      </div>
                    </template>
                  </el-tree>
                </div>
              </el-tab-pane>
              <el-tab-pane label="下属" name="second">
                <div>
                  <el-empty description="这里什么都没有" />
                </div>
              </el-tab-pane>
              <el-tab-pane label="部门" name="third">
                <div>
                  <el-empty description="这里什么都没有" /></div
              ></el-tab-pane>
            </el-tabs>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="pl-4 selected-users">
            <div
              class="flex justify-between items-center selected-users-header"
            >
              <div>
                已选人员<span v-show="selectValue.length"
                  >({{ selectValue.length }})</span
                >
              </div>
              <div>
                <el-button type="primary" link>清空已选</el-button>
              </div>
            </div>
            <div class="selected-users-body">
              <el-empty
                description="这里什么都没有"
                v-if="selectValue.length === 0"
              />
              <el-scrollbar v-else height="100%">
                <div class="user-list">
                  <div
                    class="user-item"
                    v-for="item in selectValue"
                    :key="item"
                  >
                    <div class="user-item-avatar">
                      <el-avatar
                        :size="44"
                        :src="'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png'"
                      />
                    </div>
                    <div class="user-item-inf">
                      <div class="inf-name">张三</div>
                      <div class="inf-email">94648929@qq.com</div>
                    </div>
                    <div class="user-item-action" title="移除">
                      <el-icon><Close /></el-icon>
                    </div>
                  </div>
                </div>
              </el-scrollbar>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确定</el-button
        >
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import type { TabsPaneContext } from "element-plus";
import type { TreeOptionProps } from "element-plus/es/components/tree/src/tree.type";

interface Tree {
  label: string;
  type: "dept" | "user";
  class?: string;
  children?: Tree[];
}

const value = ref("");

const options = [
  {
    value: "Option1",
    label: "Option1",
  },
  {
    value: "Option2",
    label: "Option2",
  },
  {
    value: "Option3",
    label: "Option3",
  },
  {
    value: "Option4",
    label: "Option4",
  },
  {
    value: "Option5",
    label: "Option5",
  },
];

const dialogVisible = ref(true);
const searchValue = ref("");

const handleClose = (done: () => void) => {
  ElMessageBox.confirm("Are you sure to close this dialog?")
    .then(() => {
      done();
    })
    .catch(() => {
      // catch error
    });
};

const handleNodeClick = (data: Tree) => {
  console.log(data);
};

const data: Tree[] = [
  {
    label: "Level one 1",
    type: "dept",
    children: [
      {
        label: "Level two 1-1",
        type: "dept",
        children: [
          {
            label: "Level three 1-1-1",
            type: "user",
            class: "user-item-node",
          },
          {
            label: "Level three 1-1-2",
            type: "user",
            class: "user-item-node",
          },
          {
            label: "Level three 1-1-3",
            type: "user",
            class: "user-item-node",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 2",
    type: "dept",
    children: [
      {
        label: "Level two 2-1",
        type: "dept",
        children: [
          {
            label: "Level three 2-1-1",
            type: "user",
            class: "user-item-node",
          },
        ],
      },
      {
        label: "Level two 2-2",
        type: "dept",
        children: [
          {
            label: "Level three 2-2-1",
            type: "user",
            class: "user-item-node",
          },
        ],
      },
    ],
  },
  {
    label: "Level one 3",
    type: "dept",
    children: [
      {
        label: "Level two 3-1",
        type: "dept",
        children: [
          {
            label: "Level three 3-1-1",
            type: "user",
            class: "user-item-node",
          },
        ],
      },
      {
        label: "Level two 3-2",
        type: "dept",
        children: [
          {
            label: "Level three 3-2-1",
            type: "user",
            class: "user-item-node",
          },
        ],
      },
    ],
  },
];

const defaultProps: TreeOptionProps = {
  children: "children",
  label: "label",
};

const activeName = ref("first");

const handleClick = (tab: TabsPaneContext, event: Event) => {
  console.log(tab, event);
};

const selectValue = ref<number[]>([]);
for (let index = 0; index < 100; index++) {
  selectValue.value.push(index);
}
</script>

<style lang="less" scoped>
.selected-users {
  height: 50vh;
  &-header {
    height: 40px;
  }
  &-body {
    height: calc(100% - 40px);
  }
}

.user-list {
  padding-right: 16px;
  padding-bottom: 16px;
}

.user-item {
  position: relative;
  display: flex;
  padding: 8px 5px;
  box-sizing: content-box;
  align-items: center;

  &:hover {
    background-color: var(--el-color-info-light-9);

    .user-item-action {
      display: flex;
    }
  }

  &-avatar {
    width: 44px;
    height: 44px;
    margin-right: 8px;
  }

  &-inf {
    flex: 1;
    align-items: center;

    .inf-name {
      font-size: 15px;
    }

    .inf-email {
      margin-top: 2px;
      font-size: 13px;
    }
  }
  &-action {
    position: absolute;
    right: 0;
    top: 0;
    display: none;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    background-color: var(--el-color-danger);
    color: #fff;
    text-align: center;
    line-height: 18px;
    font-size: 13px;
    cursor: pointer;

    &:hover {
      background-color: var(--el-color-danger-light-3);
    }
  }

  &.small {
    padding: 0;

    .user-item-avatar {
      width: 24px;
      height: 24px;
      margin-right: 4px;
    }

    .inf-name {
      font-size: 12px;
    }
    .inf-email {
      display: none;
    }
  }
}
</style>
